#include "lexer/lexercc.hpp"

using namespace std;

static void lexer_cmd(int argc, char *args[]);
static void lexer_demo();

int main(int argc, char *args[]) {
    // Proccess command line
    lexer_cmd(argc, args);
    fstream fin;
    __OPEN_AND_CHECK(fin, args[1], false);
    // ---------------------------------------------------------------------- //
    // Use factory method to build lexer from JSON file
    MyLexer lexer = buildLexerFromJson(fin);
    writeLexer(lexer, args[2], "lexer/");
    // ---------------------------------------------------------------------- //
    // A simple demo, uncomment it and have fun
    // lexer_demo(lexer);
    system("pause");
    return 0;
}

static void lexer_demo(const MyLexer &lexer){
    string s;
    while (true) {
        cout << "Input the string to identify: ";
        getline(cin, s);
        vector<Token> tokens;
        try {
            tokens = lexer.lex(s);
        } catch (ParseException e) {
            continue;
        }
        for (auto i : tokens) {
            cout << i.tokenName << ": " << i.word << endl;
        }
    }
}

static void lexer_cmd(int argc, char *args[]) {
    string header = "* Zeege (R) Lexical Analyzer Generator Version 1.0.0\n"
                    "* Copyright(C) Zeege. All rights reserved.\n";
    string asteroids =
        "\n*************************************************************\n";
    cout << asteroids << header << "* Welcome to MyLex! Happy lexing! >_<" << asteroids
         << "\n\n";
    string promptStr = "Invalid arguments.\nUsage: lexer_main.exe {lex definition filepath} "
                        "{lexer output dir}\n"
                        "e.g. lexer_main.exe abc.json ./abclexer\n";
    if (argc != 3) {
        cout << promptStr << endl;
        system("pause");
        exit(0);
    }
}
